<template>
  <van-cell class="film-card" clickable center @click="onDetail">
    <template #title>
      <div class="cover">
        <cover-lazy :src="film.pic" />
      </div>
    </template>
    <div class="content van-ellipsis">
      <div class="title van-ellipsis">{{ film.name }}</div>
      <div v-if="canBuy">评分：<span class="price2">{{ film.grade | formatGrade }}</span></div>
      <div v-else>
        {{ dayjs(film.publishDate).format('YYYY-MM-DD') }}
        <span class="date">{{ dayjs().to(film.publishDate, true) }}</span>后上映
      </div>
      <div>类型：{{ film.filmTypes }}</div>
      <div>导演：{{ film.director }}</div>
      <div class="van-ellipsis">主演：{{ film.cast }}</div>
    </div>
    <template #right-icon>
      <div class="buy">
        <van-button 
          round 
          size="mini" 
          :color="canBuy ? variables.primary : variables.warning" 
          @click.stop="onBuy"
        >{{ canBuy ? '购票' : '预售' }}</van-button>
      </div>
    </template>
  </van-cell>
</template>

<script>
import { getRandomId, canBuy } from '@/utils/util'
import variables from '@/styles/variables.scss'

export default {
  name: 'FilmCard',
  props: {
    film: {
      type: Object,
      default: () => {}
    }
  },
  computed: {
    variables() {
      return variables
    },
    // 是否可购买
    canBuy() { 
      return canBuy(this.film.publishDate)
    }
  },
  methods: {
    // 影片详情
    onDetail() {
      this.$router.push(`/film-detail/${this.film.filmId}?random=${getRandomId()}`)
    },
    // 购买 去影院
    onBuy() {
      // 将当前影片id 存入缓存
      this.$store.commit('global/SET_CUR_FILM_ID', this.film.filmId)
      this.$router.push('/cinema')
    }
  },
}
</script>

<style lang="scss" scoped>
.film-card {
  /deep/ .van-cell__title {
    flex: none;
  }
  .cover {
    width: 80px;
    height: 110px;
    border-radius: 10px;
    overflow: hidden;
  }
  .content {
    margin-left: 10px;
    font-size: 12px;
    line-height: 22px;
    text-align: left;
    color: $unactive;
    .title {
      font-size: 14px;
      font-weight: bold;
      color: $active;
    }
    .date {
      font-weight: 700;
      color: $warning;
    }
  }
  .buy {
    .van-button--mini {
      padding: 0 14px;
    }
  }
}
</style>